<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data {
	private $CI;
	
	public function __construct() {
		$this->CI = &get_instance();
	}
	
	public function get_story($username) {
		$this->CI->load->model('Story_Model', 'storymodel');
		return $this->CI->storymodel->get_story($username);
	}
	
	public function update_story($username, $story) {
		$this->CI->load->model('Story_Model', 'storymodel');
		if ($this->CI->authentication->is_allowed('EDIT_STORY')) {
			return $this->CI->storymodel->update_story($username, $story);
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function get_achievement($username, $offset = '', $limit = '') {
		$this->CI->load->model('Achievement_Model', 'achievementmodel');
		return $this->CI->achievementmodel->get_achievement($username, $offset, $limit);
	}
	
	public function add_achievement($username, $name, $year, $result) {
		$this->CI->load->model('Achievement_Model', 'achievementmodel');
		if ($this->CI->authentication->is_allowed('CRUD_ACHIEVEMENT')) {
			return $this->CI->achievementmodel->add_achievement($username, $name, $year, $result);
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function edit_achievement($id, $name, $year, $result) {
		$this->CI->load->model('Achievement_Model', 'achievementmodel');
		if ($this->CI->authentication->is_allowed('CRUD_ACHIEVEMENT')) {
			return $this->CI->achievementmodel->edit_achievement($id, $name, $year, $result);	
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function delete_achievement($id) {
		$this->CI->load->model('Achievement_Model', 'achievementmodel');
		if ($this->CI->authentication->is_allowed('CRUD_ACHIEVEMENT')) {
			return $this->CI->achievementmodel->delete_achievement($id);	
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function get_organization($username, $offset = '', $limit = '') {
		$this->CI->load->model('Organization_Model', 'organizationmodel');
		return $this->CI->organizationmodel->get_organization($username, $offset, $limit);
	}
	
	public function add_organization($username, $name, $year, $position) {
		$this->CI->load->model('Organization_Model', 'organizationmodel');
		if ($this->CI->authentication->is_allowed('CRUD_ORGANIZATION')) {
			return $this->CI->organizationmodel->add_organization($username, $name, $year, $position);
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function edit_organization($id, $name, $year, $position) {
		$this->CI->load->model('Organization_Model', 'organizationmodel');
		if ($this->CI->authentication->is_allowed('CRUD_ORGANIZATION')) {
			return $this->CI->organizationmodel->edit_organization($id, $name, $year, $position);	
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function delete_organization($id) {
		$this->CI->load->model('Organization_Model', 'organizationmodel');
		if ($this->CI->authentication->is_allowed('CRUD_ORGANIZATION')) {
			return $this->CI->organizationmodel->delete_organization($id);	
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function count_organization($username) {
		$this->CI->load->model('Organization_Model', 'organizationmodel');
		return $this->CI->organizationmodel->count_organization($username);
	}
	
	public function count_achievement($username) {
		$this->CI->load->model('Achievement_Model', 'achievementmodel');
		return $this->CI->achievementmodel->count_achievement($username);
	}
	
	public function get_organization_by_id($id) {
		$this->CI->load->model('Organization_Model', 'organizationmodel');
		return $this->CI->organizationmodel->get_organization_by_id($id);	
	}
	
	public function get_achievement_by_id($id) {
		$this->CI->load->model('Achievement_Model', 'achievementmodel');
		return $this->CI->achievementmodel->get_achievement_by_id($id);
	}
	
	public function get_student($username) {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->get_student($username);
	}
	
	public function get_student_by_id($id) {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->get_student_by_id($id);
	}
	
	public function get_student_by_id_for_sponsor($id, $sponsor) {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->get_student_by_id_for_sponsor($id, $sponsor);
	}
	
	public function list_year($database = FALSE) {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->list_year($database);
	}
	
	public function list_major() {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->list_major();
	}
	
	public function search_student($offset, $limit, $name = '', $year = '', $major = '', $gpa = 0.00) {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->search_student($offset, $limit, $name, $year, $major, $gpa);
	}
	
	public function count_student($name = '', $year = '', $major = '') {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->count_student($name, $year, $major);
	}
	
	public function search_student_for_sponsor($offset, $limit, $sponsor, $name = '', $year = '', $major = '', $scholarship = '') {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->search_student_for_sponsor($offset, $limit, $sponsor, $name, $year, $major, $scholarship);
	}
	
	public function count_student_for_sponsor($sponsor, $name = '', $year = '', $major = '', $scholarship = '') {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->count_student_for_sponsor($sponsor, $name, $year, $major, $scholarship);
	}
	
	public function get_histories($username) {
		$this->CI->load->model('Student_Model', 'studentmodel');
		return $this->CI->studentmodel->get_histories($username);
	}
}